package com.lzdongrui.backend.service.impl;

import com.lzdongrui.backend.domain.Eliminate;
import com.lzdongrui.backend.domain.Instructor;
import com.lzdongrui.backend.domain.dto.EliminateDTO;
import com.lzdongrui.backend.domain.vo.EliminateVO;
import com.lzdongrui.backend.mapper.ApplyLeaveMapper;
import com.lzdongrui.backend.mapper.EliminateMapper;
import com.lzdongrui.backend.mapper.InstructorMapper;
import com.lzdongrui.backend.mapper.StudentinfoMapper;
import com.lzdongrui.backend.service.IEliminateService;
import com.lzdongrui.common.core.domain.entity.ClientUser;
import com.lzdongrui.common.core.domain.entity.SysDept;
import com.lzdongrui.common.utils.DateUtils;
import com.lzdongrui.common.utils.SecurityUtils;
import com.lzdongrui.common.utils.snowid.SnowflakeIdWorker;
import com.lzdongrui.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.lzdongrui.common.utils.PageUtils.startPage;

/**
 * 销假记录Service业务层处理
 *
 * @author ysl
 * @date 2023-06-09
 */
@Service
public class EliminateServiceImpl implements IEliminateService {
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private EliminateMapper eliminateMapper;

    @Autowired
    private StudentinfoMapper studentinfoMapper;

    @Autowired
    private ApplyLeaveMapper applyLeaveMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    @Autowired
    private InstructorMapper instructorMapper;

    /**
     * 查询销假记录
     *
     * @param id 销假记录主键
     * @return 销假记录
     */
    @Override
    public EliminateVO selectEliminateById(Long id) {
        return eliminateMapper.selectEliminateById(id);
    }

    /**
     * 查询销假记录列表
     *
     * @param eliminate 销假记录
     * @return 销假记录
     */
    @Override
    public List<EliminateVO> selectEliminateList(EliminateDTO eliminate) {
        List<EliminateVO> retList = new ArrayList<>();

        //判别这个人在的部门是不是最顶级 最顶级及就直接去查所有数据
        SysDept depInfo = deptMapper.selectDeptLeaveById(eliminate.getUserDepId());

        // 管理员身份 查询所有的数据
        if (depInfo.getParentId() == 0) {
            startPage();
            retList = eliminateMapper.selectEliminateList(eliminate);
        } else {
            //查询当前查看人是否是辅导员
            Instructor instructor = new Instructor();
            instructor.setInstructor(eliminate.getUserid());
            List<Instructor> insList = instructorMapper.selectInstructorListInApplyLeave(instructor);

            if (insList.size() != 0) {
                //是辅导员
                startPage();
                retList = eliminateMapper.selectEliminateistInstructor(eliminate);
            } else {
                //非辅导员 但属于该学院人员
                eliminate.setCollegeId(depInfo.getCollegeId());
                startPage();
                retList = eliminateMapper.selectEliminateistByCollege(eliminate);
            }
        }
        return retList;
    }

    /**
     * 新增销假记录
     *
     * @param eliminate 销假记录
     * @return 结果
     */
    @Override
    public int insertEliminate(Eliminate eliminate) {
        // 储存添加数据的条数
        int eliminateData = 0;
        Long stuId = SecurityUtils.getClientUserId();
        Date nowTime = DateUtils.getNowDate();
        EliminateDTO eliminateDTO = new EliminateDTO();
        eliminateDTO.setStuId(stuId);
        eliminateDTO.setBeginTime(nowTime);
        List<EliminateVO> leaveList = eliminateMapper.selectStuStayEliminateList(eliminateDTO);
        eliminate.setCreateTime(nowTime);
        for (EliminateVO item : leaveList) {
            eliminate.setId(snowflakeIdWorker.nextId());
            eliminate.setApplyLeaveId(item.getApplyLeaveId());
            eliminateMapper.insertEliminate(eliminate);
            eliminateData++;
        }
        applyLeaveMapper.updateLeaveToEliminate(stuId, nowTime);
        studentinfoMapper.upStuIsAtSchoolByStudentId(stuId, 0);// 更新学生在校状态为在校
        ClientUser user = SecurityUtils.getClientLoginUser().getClientUser();
        SecurityUtils.getClientLoginUser().setClientUser(user);
        return eliminateData;
    }

    /**
     * 修改销假记录
     *
     * @param eliminate 销假记录
     * @return 结果
     */
    @Override
    public int updateEliminate(Eliminate eliminate) {
        eliminate.setUpdateTime(DateUtils.getNowDate());
        return eliminateMapper.updateEliminate(eliminate);
    }

    /**
     * 批量删除销假记录
     *
     * @param ids 需要删除的销假记录主键
     * @return 结果
     */
    @Override
    public int deleteEliminateByIds(Long[] ids) {
        return eliminateMapper.deleteEliminateByIds(ids);
    }

    /**
     * 删除销假记录信息
     *
     * @param id 销假记录主键
     * @return 结果
     */
    @Override
    public int deleteEliminateById(Long id) {
        return eliminateMapper.deleteEliminateById(id);
    }
}
